package com.cfl.ticket.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cfl.model.City;
import com.cfl.model.Ticket;
import com.cfl.model.TicketNumber;
import com.cfl.ticket.feign.TicketFeign;
import com.cfl.ticket.service.TicketService;
import com.cfl.utils.AppResponse;
import com.cfl.utils.Constant;

@Service
public class TicketServiceImpl implements TicketService{

	@Autowired
	private TicketFeign ticketFeign;
	
	
	public ArrayList<Ticket> findMyTickets(String uid, Integer page) throws Exception {
		try {
			return ticketFeign.findMyTickets(uid, page);
		} catch (Exception e) {
			throw e;
		}
	}
	
	public TicketNumber findTicketnumberByNumber(String number) throws Exception {
		try {
			return ticketFeign.findTicketnumber(number);
		} catch (Exception e) {
			throw e;
		}
	}
	
	public TicketNumber findTicketnumberByTid(String tid) throws Exception {
		try {
			return ticketFeign.findTicketnumberByTid(tid);
		} catch (Exception e) {
			throw e;
		}
	}
	
	public ArrayList<TicketNumber> findTicketnumberByAll() throws Exception {
		try {
			return ticketFeign.findTicketnumberByAll();
		} catch (Exception e) {
			throw e;
		}
	}
	
	public AppResponse addTicket(Ticket ticket) throws Exception {
		try {
			if(ticket == null || 
					StringUtils.isBlank(ticket.getStartCity()) || 
					StringUtils.isBlank(ticket.getEndCity()) ||
					StringUtils.isBlank(ticket.getDriveTimeText()) ||
					StringUtils.isBlank(ticket.getNumber()) || 
					StringUtils.isBlank(ticket.getSeat()) || 
					StringUtils.isBlank(ticket.getSeatType()) || 
					StringUtils.isBlank(ticket.getSellCity()) || 
					StringUtils.isBlank(ticket.getUid()) ||
					StringUtils.isBlank(ticket.getBuyName()) ||
					ticket.getPrice() == null)
			{
				return new AppResponse(Constant.code_error, Constant.parames_not_all);
			}
			
			// 时间
			String timeText = ticket.getDriveTimeText();
			if(timeText.contains("年") && timeText.contains("月") && timeText.contains("日"))
			{
				int yearIndex = timeText.indexOf("年");
				int monthIndex = timeText.indexOf("月");
				int dayIndex = timeText.indexOf("日");
				String year = timeText.substring(0, yearIndex);
				String month = timeText.substring(yearIndex+1, monthIndex);
				String day = timeText.substring(monthIndex+1, dayIndex);
				
				String timeStr =  year + "-" + month + "-" + day;
				System.out.println(timeStr);
				SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
				Date date = format.parse(timeStr);
				
				ticket.setDriveTime(date);
			}
			// 城市
			String startCityStr = ticket.getStartCity();
			String endCityStr = ticket.getEndCity();
			City startCity = ticketFeign.findCityByName(ticket.getStartCity().substring(0, startCityStr.length()-1));
			City endCity = ticketFeign.findCityByName(ticket.getEndCity().substring(0, endCityStr.length()-1));
			if(startCity == null)
			{
				return new AppResponse("-1", "出发城市错误");
			}
			if(endCity == null)
			{
				return new AppResponse("-1", "目的城市错误");
			}
			// 车次
			TicketNumber ticketNumber = ticketFeign.findTicketnumber(ticket.getNumber());
			if(ticketNumber == null)
			{
				return new AppResponse("-1", "车次错误");
			}
			ticket.setNumber(ticketNumber.getTid());
			ticket.setStartCity(startCity.getCid());
			ticket.setEndCity(endCity.getCid());
			ticketFeign.addTicket(ticket);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new AppResponse();
	}
}
